<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>JS表达式运算</title>
</head>
<body>
  <h1>请使用Chrome并按F12进行调试和测试</h1>
  <pre>
    var context = new CalContext();
    //增加值栈——变量
    context.putData("$a",5);
    //增加值栈——无参数函数
    context.putFunction("fun0",function(){ return 1000;});
    //增加值栈——单参数函数
    context.putFunction("fun1",function(a){ return 10 * a;});
    //增加值栈——多参数函数
    context.putFunction("fun2",function(a,b){ return 10 * a + b;});
    //增加值栈——对象参数函数
    var t = {
      year: 28,
      fun: function(a) {
        return a + this.year;
      }
    };
    context.putFunction("fun3",t.fun,t);
    //增加值栈——对象
    context.putData("people",{
       year: 28,
       fun1: function(){
         return this.year + 2;
       },
       fun2: function(a){
         return this.year + 10 * a;
       },
       fun3: function(a,b){
         return this.year + 10 * a + b;
       }
    });

    //获取值
    //var result = context.calc(expr);

    //下面是测试用例

    context.test("40+8*2",56);

    context.test("40*8+2",322);

    context.test("40/8+2",7);   

    context.test("40+8/2",44);

    context.test("(40+8)/2",24);

    context.test("40*(8-2)",240);

    context.test("10%3 + 1",2);

    context.test("0.1 + 1 + 0.3",1.4);

    context.test("0.1 *10 + 6.3",7.3);

    context.test("$a + 5",10);  

    context.test("($a + 1)/2 + 1",4);

    context.test("$a + fun0()",1005);

    context.test("$a + fun0() + 100",1105);

    context.test("$a + fun1(1)",15);

    context.test("$a + fun2(2,1)",26);

    context.test("$a + fun2(3+7,3)",108);

    context.test("$a + fun2(fun1(2),3) + 20",228);

    context.test("$a + fun3(2)",35);

  </pre>
<script type="text/javascript" src="expression.js"></script>
<script>
    var context = new CalContext();
    //增加值栈——变量
    context.putData("$a",5);
    //增加值栈——无参数函数
    context.putFunction("fun0",function(){ return 1000;});
    //增加值栈——单参数函数
    context.putFunction("fun1",function(a){ return 10 * a;});
    //增加值栈——多参数函数
    context.putFunction("fun2",function(a,b){ return 10 * a + b;});
    //增加值栈——对象参数函数
    var t = {
      year: 28,
      fun: function(a) {
        return a + this.year;
      }
    };
    context.putFunction("fun3",t.fun,t);
    //增加值栈——对象
    context.putData("people",{
       year: 28,
       fun1: function(){
         return this.year + 2;
       },
       fun2: function(a){
         return this.year + 10 * a;
       },
       fun3: function(a,b){
         return this.year + 10 * a + b;
       }
    });

    context.test("40+8*2",56);

    context.test("40*8+2",322);

    context.test("40/8+2",7);

    context.test("40+8/2",44);

    context.test("(40+8)/2",24);

    context.test("40*(8-2)",240);

    context.test("10%3 + 1",2);

    context.test("0.1 + 1 + 0.3",1.4);

    context.test("0.1 *10 + 6.3",7.3);

    context.test("$a + 5",10);

    context.test("($a + 1)/2 + 1",4);

    context.test("$a + fun0()",1005);

    context.test("$a + fun0() + 100",1105);

    context.test("$a + fun1(1)",15);

    context.test("$a + fun2(2,1)",26);

    context.test("$a + fun2(3+7,3)",108);

    context.test("$a + fun2(fun1(2),3) + 20",228);

    context.test("$a + fun3(2)",35);
</script>
</body>
</html>